/**
 * Created by zhoulin on 2019/3/31.
 */
$(function () {
    //查询菜单模块
    //ajax(0)
})
var app = new Vue({
    el: '#wrapper',
    data: {
        iframeSrc:"home.html",
        toggleIndex:0,
        userInfo:{username:"",nickname:""},
        menuList:[{id:"1",icon:"fa fa-refresh",name:"系统权限加载中",pid:"0",url:"../login.html",children:[]}],
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if(empty(token)){
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon:5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            },1200)

            return ;
        }

        //查询登录人信息
        that.getUserInfo();
        //查询菜单模块
        that.getMenu(0);

    },
    methods: {
        // 修改密码
        updateWp: function() {
            layer.open({
                type: 1,
                title: '修改密码',
                area: ['500px','300px'],
                content: $('#updatePasswd')
            });
        },
        // 确定
        submitPwd: function() {
            var that = this;
            var oldPassword = $('input[name="oldPassword"]').val();
            if (empty(oldPassword)) {
                layer.msg('请输入原始密码', {time: 1500, icon: 5});
                return;
            }
            var password = $('input[name="password"]').val();
            if (empty(password)) {
                layer.msg('请输入新的密码', {time: 1500, icon: 5});
                return;
            }
            var confirmPassword = $('input[name="confirmPassword"]').val();
            if (empty(confirmPassword)) {
                layer.msg('请输入确认密码', {time: 1500, icon: 5});
                return;
            }
            if (password != confirmPassword) {
                layer.msg('新密码与确认密码不匹配！', {time: 1500, icon: 5});
                return;
            }
            Post(that, _HOST + _API.admin_index_updatePs + "?oldPassword=" + oldPassword + "&password=" + password, {}, true, '')
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        if (response.success) {
                            layer.closeAll();
                            layer.msg(response.msg, {time: 1500, icon: 1});
                        } else {
                            layer.msg(response.msg, {time: 1500, icon: 5});
                        }
                    }
                });
        },
        //退出登录
        loginOut:function () {
            layer.msg('确认退出登录？', {
                time: 0 //不自动关闭
                ,shade: 0.5
                ,btn: ['退出', '取消']
                ,yes: function(index){
                    sessionStorageUtils.del(storageKey.sessionStorageKey.token);
                    sessionStorageUtils.del(storageKey.sessionStorageKey.permCodes);
                    layer.close(index);
                    //加载层-风格4
                    var load = layer.msg('退出中。。。', {
                        time: 0 //不自动关闭
                        ,icon: 16
                        ,shade: 0.5
                    });

                    setTimeout(function () {
                        layer.close(load);
                        window.location.href = "../login.html";
                    },1500)
                }
            });
        },
        //获取用户基本信息
        getUserInfo:function () {
            var that = this;
            Get(that,_HOST+_API.admin_sysAdmin_getUserInfo)
                .then(function (response) {
                    // 响应成功回调
                    //console.log(response)
                    if(response){
                        if(response.success){
                            //layer.msg("查询成功！");
                            that.userInfo = response.data;
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                })
        },
        //查询菜单目录
        getMenu:function (pid) {
            var that = this;
            Get(that,_HOST+_API.admin_index_getMenu+pid)
                .then(function (response) {
                    // 响应成功回调
                    //console.log(response)
                    if(response){
                        if(response.success){
                            //layer.msg("查询成功！");
                            if(0 == pid){
                                that.menuList = response.list;
                                if(that.menuList.length > 0){
                                    that.getMenu(that.menuList[0].id);
                                }
                                //ajax(that.menuList[0].id);
                            }else {
                                li(response.list)
                            }
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                })
        },
        toggle:function (id,index) {
            var that = this;
            that.toggleIndex = index;
            that.getMenu(id);
            //ajax(id);
        }
    },
})

function submitPWD() {
    app.submitPwd();
}

/**
 * 动态构建权限菜单
 * @param list
 */
function li(list) {
    $("#side-menu>li").nextAll().remove();
    var liHtml = ''
    for (var i = 0; i<list.length;i++){
        liHtml = liHtml + '<li><a class="'+(list[i].children.length>0 ? '' : 'J_menuItem')+'" href="'+(list[i].children.length>0 ? 'javascript:;' : list[i].url)+'" onclick="clickMenuChildren(this)"><i class="'+list[i].icon+'"></i><span class="nav-label">'+list[i].name+'</span>';
        if(list[i].children.length > 0 ){
            liHtml = liHtml + '<span class="fa arrow"></span></a><ul class="nav nav-second-level collapse">';
            for (var j = 0; j<list[i].children.length;j ++){
                liHtml = liHtml + '<li><a class="J_menuItem" href="'+list[i].children[j].url+'"><i class="'+list[i].children[j].icon+'"></i>'+list[i].children[j].name+'</a></li>'
            }
            liHtml = liHtml + '</ul>'

        }else {
            liHtml = liHtml + '</a>';
        }
        liHtml = liHtml+'</li>';
    }
    $("#side-menu").append(liHtml)
    new_element=document.createElement("script");
    new_element.setAttribute("type","text/javascript");
    new_element.setAttribute("src","../../../public-tools/pc/js/contabs.min.js");
    document.body.appendChild(new_element);
}

/**
 * 菜单点击样式跟换
 * @param that
 */
function clickMenuChildren(that) {
    $(that).parent("li").siblings('li').removeClass('active');  // 删除其他兄弟元素的样式
    $(that).parent("li").siblings('li').children("ul").removeClass('in');  // 删除其他兄弟元素的样式
    $(that).next("ul").addClass('in');  // 删除其他兄弟元素的样式
    if($(that).parent("li").hasClass("active")){
        $(that).parent("li").removeClass("active");
        $(that).next("ul").removeClass('in');  // 删除其他兄弟元素的样式
    }else{
        $(that).parent("li").addClass("active");
    }
}

/**
 * 备用
 * @param pid
 */
function ajax(pid) {
    var index = layer.load();
    $.ajax({
        async: true,
        url: _HOST+_API.admin_index_getMenu+pid,
        type: "get",
        dataType: 'json',
        headers:{Authorization:sessionStorageUtils.getJson(storageKey.sessionStorageKey.token)},
        success: function (data) {
            layer.close(index);
            if (data.success) {
                console.log(data);
                if(0 == pid){
                    app.menuList = data.list;
                    //that.getMenu(that.menuList[0].id);
                    ajax(app.menuList[0].id);
                }else {
                    li(data.list)
                }
            }
        }
    });
}
